
<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>支付配置</cite></a>
    <a><cite>接口类型</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-tab layui-tab-brief">
      <ul class="layui-tab-title">
        <li class="layui-this">商户列表</li>
      </ul>
      <div class="layui-tab-content">
        <div class="layui-tab-item layui-show">
          <div class="layui-row">
            <div class="layui-form" style="float:right;">
              <div class="layui-form-item" style="margin:0;">
                <div class="layui-input-inline">
                  <input type="text" name="searchSubMchId" id="searchSubMchId" placeholder="商户ID" autocomplete="off" class="layui-input">
                </div>
                <a id="search" class="layui-btn" data-type="reload"><i class="layui-icon layui-icon-search"></i>搜索</a>
              </div>
            </div>
          </div>
          <div class="layui-row">
            <div class="layuiAdmin-btns" style="margin-bottom: 10px;">
              <a class="layui-btn mchAddBtn"><i class="layui-icon layui-icon-add-1"></i>申请商户进件</a>
              <a class="layui-btn layui-btn-normal" id="chart"><i class="layui-icon layui-icon-chart"></i>收款数据</a>
              <a class="layui-btn layui-btn-warm" id="btnBack"><i class="layui-icon layui-icon-return"></i>返回</a>
          </div>
        </div>
        <table id="XxPay_Mgr_Merchant_dataAll" lay-filter="XxPay_Mgr_Merchant_dataAll"></table>
        </div>
      </div>
    </div>
  </div>
</div>

<script>
  layui.use(['admin', 'table', 'util'], function(){
    var $ = layui.$
            ,admin = layui.admin
            ,table = layui.table
            ,element = layui.element
            ,form = layui.form
            ,view = layui.view;

    element.render('breadcrumb', 'breadcrumb');
    var passageTemplateId = layui.router().search.id;

    var ifTypeCode = layui.router().search.ifTypeCode;

    var tplStatus = function(d){
        var checkedHtml = " subMchId='"+d.subMchId+"' ";
        if(d.status == 1) {
            checkedHtml += " checked";
        }
        return '<input type="checkbox" lay-filter="statusSwitch" lay-skin="switch" '+checkedHtml+' lay-text="启用|停用">';
    };
    var tplApplyStatus = function(d){
        if(d.applyStatus == 1) {
            return "未进件";
        }else if (d.applyStatus == 2) {
            return "<span style='color: darkorange'>申请中</span>";
        }else if (d.applyStatus == 3) {
            return "<span style='color: #3292FB'>待签约</span>";
        }else if (d.applyStatus == 98) {
            return "<span style='color: red'>申请失败</span>";
        }else if (d.applyStatus == 99) {
            return "<span style='color: green'>正常</span>";
        }
    };

    var tplEdit = function(d){
        var applyUpdate = "<a class='layui-btn layui-btn-normal layui-btn-xs' lay-event='update'><i class='layui-icon layui-icon-edit'></i>进件修改</a>";
        if (d.applyStatus == 2 || d.applyStatus == 3 || d.applyStatus == 99) {
            applyUpdate = "<a class='layui-btn layui-btn-normal layui-btn-xs layui-bg-gray' disabled='true'><i class='layui-icon layui-icon-edit'></i>进件修改</a>";
        }
        return  applyUpdate + "<a class='layui-btn layui-btn-primary layui-btn-xs' lay-event='risk'><i class='layui-icon layui-icon-engine'></i>风控</a>"+
              "<a class='layui-btn layui-btn-xs' lay-event='query'><i class='layui-icon layui-icon-refresh'></i>进件状态</a>" +
              "<a class='layui-btn layui-btn-xs layui-btn-danger' lay-event='del'><i class='layui-icon layui-icon-delete'></i>删除</a>";
    };

    var tplRemark = function(d) {
        if (d.applyStatus == 98) {
            return d.rejectRemark;
        }else {
            return "";
        }
    }

    var tplapplyQueryInfo = function(d){
      if(!d.applyQueryInfo){
        return "";
      }

      let array = JSON.parse(d.applyQueryInfo);

      let result = "";
      $.each(array, function(){

        result += `<div>查询时间： ${this.queryTime} <br/>查询信息： ${JSON.stringify(this.queryResult)} <hr/></div>`;

      });

      return result;
    }


    //商户列表
    table.render({
      elem: '#XxPay_Mgr_Merchant_dataAll'
      ,url: layui.setter.baseUrl + '/config/pay_alizft_mchInfo/list' //列表接口
      ,where: {
          access_token: layui.data(layui.setter.tableName).access_token,
          passageTemplateId: passageTemplateId
      }
      ,id: 'tableReload'
      ,page: true
      ,cols: [[
        {type: 'checkbox', fixed: 'left'}
        ,{field: 'smid', title: '子商户ID'}
        ,{field: 'name', title: '商户名称'}
        ,{field: 'dayAmount', title: '今日收款', templet: '<div>{{ d.dayAmount ? d.dayAmount/100 : 0 }}</div>'}
        ,{field: 'status', title: '系统状态', templet: tplStatus}
        ,{field: 'applyStatus', title: '进件状态', templet: tplApplyStatus}
        ,{field: 'applyQueryInfo', title: '进件查询信息', templet: tplapplyQueryInfo}
        ,{field: 'rejectRemark', title: '审核失败信息', templet: tplRemark}
        ,{field: 'edit', title: '操作', width: 320, fixed: 'right', templet: tplEdit }
      ]]
        ,skin: 'line'
    });

    //监听工具条
    table.on('tool(XxPay_Mgr_Merchant_dataAll)', function(obj){
      var data = obj.data;
      if(obj.event === 'update'){
          view.xxpayPopup("进件编辑", "config/pay_interface_type/template/sub_mch_list/zftApply", {subMchId: data.subMchId, passageTemplateId: passageTemplateId}, {btn: false});
      }else if(obj.event === 'query'){
          admin.req({
              type: "POST",
              url: layui.setter.baseUrl + '/config/pay_alizft_mchInfo/query',
              data: {subMchId: data.subMchId},
              success: function(res){
                  layer.msg(res.msg);
                  if(res.code == 0) {
                      table.reload('tableReload');
                  }
              }
          });
      }else if(obj.event === 'risk'){
          view.xxpayPopup("风控", "config/pay_interface_type/template/sub_mch_list/risk_set", {subMchId: data.subMchId}, {area:['60%','auto']});
      } else if (obj.event === 'del') {
          //打开弹层
          var thisLayerId = layer.confirm('确认删除此二级商户吗？', {icon: 3, title:'提示'}, function(index){
              layer.close(thisLayerId);
              var layerMsgId = layer.msg('正在执行删除任务... ',{time: 0, shade: 0.3});
              admin.req({
                  type: 'POST',
                  url: layui.setter.baseUrl + '/config/pay_alizft_mchInfo/delete',
                  data: {subMchId: data.subMchId},
                  success: function (res) {
                      if(res.code == 0){ //成功创建任务
                          layer.close(layerMsgId);
                          table.reload('tableReload');  //刷新页面
                          layer.msg("删除成功！");
                      }
                  },
                  complete:function(){
                      layer.close(layerMsgId);
                  }
              });
          });
      }
    });

      form.on('switch(statusSwitch)', function(data){

          var subMchId = $(data.elem).attr('subMchId');
          var updateState = data.elem.checked ? "1" : "0";
          var loadingIndex = layer.load();
          admin.req({
              type: "POST",
              url: layui.setter.baseUrl + '/config/pay_alizft_mchInfo/updateStatus',
              data: {subMchId: subMchId, status: updateState},
              success: function(res){
                  if(res.code == 0) {
                      table.reload('tableReload');
                  }
              },
              complete:function(){
                  layer.close(loadingIndex);
              }
          });
      });

    // 搜索
    var $ = layui.$, active = {
      reload: function(){
        var subMchId = $('#searchSubMchId').val();
        //执行重载
        table.reload('tableReload', {
          page: {
            curr: 1 //重新从第 1 页开始
          }
          ,where: {
              subMchId: subMchId
          }
        });
      }
    };
    $('#search').on('click', function() {
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });

    $('.mchAddBtn').click(function(){
        view.xxpayPopup("新增二级商户", "config/pay_interface_type/template/sub_mch_list/zftApply", {passageTemplateId: passageTemplateId}, {btn: false});
    });

    $('#btnBack').on('click', function() {
        location.hash = '/config/pay_interface_type/template//ifTypeCode=' + ifTypeCode;
    });

    $("#chart").on('click', function () {
        view.xxpayPopup("收款数据", "config/pay_interface_type/template/sub_mch_list/chart", {passageTemplateId: passageTemplateId}, {btn: false});
    });

    // 渲染表单
    form.render();
  });
</script>
